var util = require('../../utils/util.js');
var api = require('../../config/api.js');
const pay = require('../../services/pay.js');
Page({
  data:{
    orderList: [],
    page: 1,
    size: 10,
    loadmoreText: '正在加载更多数据',
    nomoreText: '全部加载完成',
    nomore: false,
    totalPages: 1
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    // 页面显示

    wx.showLoading({
      title: '加载中...',
      success: function () {

      }
    });
    this.getOrderList();
  },

  /**
       * 页面上拉触底事件的处理函数
       */
  onReachBottom: function () {
    this.getOrderList()
  },

  getOrderList(){
    let that = this;

    if (that.data.totalPages <= that.data.page - 1) {
      that.setData({
        nomore: true
      })
      return;
    }

    util.request(api.OrderList+'?Page='+that.data.page+'&size='+that.data.size).then(function (res) {
      if (res.resultCode === 0) {
        that.setData({
          orderList: that.data.orderList.concat(res.data.orderList),
          page: res.data.currentPage + 1,
          totalPages: res.data.totalPages
        });
        console.log(that.data.orderList);
        wx.hideLoading();
      }
    });
  },
  payOrder(event){
      let that = this;
      let orderIndex = event.currentTarget.dataset.orderIndex;
      let order = that.data.orderList[orderIndex];
      pay.payOrder(parseInt(order.order.id)).then(res => {
        wx.redirectTo({
          url: '/pages/payResult/payResult?status=1&orderId=' + order.order.id
        });
      }).catch(res => {
        util.showErrorToast('支付失败');
      });
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){

  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})